<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>绑定手机</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/style.css" rel="stylesheet" />
		<link href="../css/account.css" rel="stylesheet"/>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">绑定手机</h1>
		</header>
		<div class="mui-content">
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label>手机</label>
					<input id='tele' type="tel" class="mui-input-clear mui-input" placeholder="+86" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v()" />
					<a id="getCheckCode" class="check-code">获取验证码</a>
				</div>
				<div class="mui-input-row">
					<label>验证码</label>
					<input id='code' type="number" class="mui-input-clear mui-input" placeholder="短信验证码"/>
				</div>
			</div>
			<div class="mui-content-padded">
				<button id='bindPhone' class="mui-btn mui-btn-block mui-btn-danger">立即绑定</button>
			</div>
			<div class="mui-content-padded">
				<p>提示：为了您的账户安全，请及时绑定手机</p>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/app.js"></script>
		<script>
			mui.init({swipeBack: true});
			mui.plusReady(function() {
				var tele=document.getElementById('tele'),
					code=document.getElementById('code'),
					getCheckCode=document.getElementById('getCheckCode'),
					time,
					flag=false;
				
				document.getElementById('getCheckCode').addEventListener('tap',function(){
					if(flag)
						return;
					if(tele.value.length != 11){
						plus.nativeUI.toast('手机号码为11位');
						return;
					}
					var w=plus.nativeUI.showWaiting('',{padlock:true});
					mui.ajax(URL+'api/UserCenter/SendCode',{
						data:JSON.stringify({
							phoneNum:tele.value
						}),
						dataType:'json',
						contentType:'application/json',
						type:'post',
						timeout:10000,
						success:function(data){
							w.close();
							if(data.success){
								plus.nativeUI.toast(data.msg);
								flag=true;
								var total=60;
								time=setInterval(function(){
									if(total>0){
										total--;
										getCheckCode.innerText=total+'s后可重发';
									}else{
										getCheckCode.innerText='重新发送';
										clearInterval(time);
										flag=false;
									}
								},1000)
							}else{
								plus.nativeUI.toast(data.msg);
							}
						},
						error:function(xhr,type,errorThrown){
							w.close();
							plus.nativeUI.toast('获取验证码失败，请检查网络')
						}
					});
				});
				
				document.getElementById('bindPhone').addEventListener('tap',function(){
					if(tele.value.length < 11){
						plus.nativeUI.toast('手机号码不得少于11位');
						return;
					}
					var w=plus.nativeUI.showWaiting('',{padlock:true});
					mui.ajax(URL+'api/UserCenter/CheckCode',{
						data:JSON.stringify({
							phoneNum:tele.value,
							code:code.value,
						}),
						dataType:'json',
						contentType:'application/json',
						type:'post',
						timeout:10000,
						success:function(data){
							w.close();
							if(data.success){
								plus.nativeUI.toast(data.msg);
								setTimeout(function(){plus.webview.currentWebview().close()},1000);
							}else{
								plus.nativeUI.toast(data.msg);
							}
						},
						error:function(xhr,type,errorThrown){
							w.close();
							plus.nativeUI.toast('绑定手机失败，请检查网络')
						}
					});
				});
			});
			
		</script>
	</body>

</html>